package 链表题.删除有序链表重复节点_一个不留;


import 链表题.ListNode;

/**
 * 方法二: 非递归方法
 */

public class Test2 {

    public static ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode s = new ListNode(-1, head);
        ListNode p1 = s;
        ListNode p2 = head;
        ListNode p3 = head.next;
        while (p3 != null) {
            if (p2.val == p3.val) {
                while (p3 != null && p2.val == p3.val) {
                    p3 = p3.next;
                }
                p1.next = p3;
                p1 = p3;
            } else {
                p1 = p1.next;
            }
            if (p1 != null) {
                p2 = p1.next;
                p3 = p2.next;
            }
        }
        return s.next;
    }


    public static void main(String[] args) {
        ListNode o = ListNode.of(0,0,1,2,3,4,4);
        System.out.println(o);
        System.out.println(deleteDuplicates(o));
    }

}
